/**
 * 项目名：化工企业安全生产信息化管理平台
 * 日期：    2020-03-01 21:14:38
 * Copyright (c) 2015- -版权所有
 */

package com.joysuch.wwyt.common.repository;

import com.joysuch.wwyt.common.entity.CommonType;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.querydsl.QuerydslPredicateExecutor;

import java.util.List;

/**
 * CommonTypeDao
 */
public interface CommonTypeDao extends JpaRepository<CommonType, Long>, QuerydslPredicateExecutor<CommonType> {
    public Page<CommonType> findAll(Specification<CommonType> spec, Pageable pageable);

    public CommonType save(CommonType bean);

    public void delete(CommonType bean);

    @Query("select count(m) from CommonType m where code = ?1")
    long countOfCode(String code);

    @Query("select count(m) from CommonType m where type=?1 and name = ?2")
    long countOfName(String type, String name);

    @Query("select count(m) from CommonType m where type=?1 and name = ?2 and id != ?3")
    long countOfNameExcludeId(String type, String name, Long id);

    @Query("select count(m) from CommonType m where code = ?1 and id != ?2")
    long countOfCodeExcludeId(String code, Long id);

    @Query("select name from CommonType m where  id = ?1")
    public String getNameById(Long id);

    @Query("select m from CommonType m where m.type=?1 and m.name = ?2")
    List<CommonType> getListByTypeAndName(String type, String name);

    @Query("select m from CommonType m where  m.name=?1 and m.type ='试题'")
    CommonType findQuestionTypeName(String questionTypeName);

    @Query("select m from CommonType m where  m.name=?1 and m.type ='试题'")
    List<CommonType> findQuestionTypeNames(String questionTypeName);


    @Query("select m from CommonType m where  m.type= ?1 and m.name=?2 ")
    CommonType findQuestionTypeAndName(String type,String name);

    @Query(value = "select m.* from wwyt_common_type m where m.id in (?1)",nativeQuery = true)
    List<CommonType> findByIds(List<Long> ids);
}
